/*********************************
 * for android project
 * input: *.xml(the layout file)
 * code generated:
 * 
 * 
 * 
 * private TextView mViewTitle;
 * 
 * private void fetchView(){
 *     mViewTitle = (TextView) findViewById(R.id.title);
 * }
 *********************************/

package android.autocode.fetchview;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.autocode.Cmd;

public class FetchView implements Cmd {
	public String CMD = "fetchview";
	public String HINT = CMD + " layout.xml";

	@Override
	public String getCmd() {
		return CMD;
	}

	@Override
	public String getHint() {
		return HINT;
	}

	@Override
	public void genCode(String[] args) {
		if (args == null || args.length < 1) {
			System.out.println(HINT);
			return;
		}

		InputStream inputStream = null;
		try {
			File layoutFile = new File(args[0]);
			inputStream = new FileInputStream(layoutFile);

			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();

			Document document = builder.parse(inputStream);
			Element root = document.getDocumentElement();

			ArrayList<String> viewList = new ArrayList<String>();
			ArrayList<String> idList = new ArrayList<String>();
			ArrayList<String> varList = new ArrayList<String>();

			fetchChildId(root, viewList, idList);

			for (int i = 0; i < viewList.size() && i < idList.size(); i++) {
				String viewName = viewList.get(i);
				String idName = idList.get(i);

				String first = idName.substring(0, 1);
				String var = "mView"
						+ idName.replaceFirst(first, first.toUpperCase());
				varList.add(var);

				System.out.println("private " + viewName + " " + var + ";");
			}

			System.out.println("private void fetchView(){");

			for (int i = 0; i < viewList.size() && i < idList.size(); i++) {
				String viewName = viewList.get(i);
				String idName = idList.get(i);
				String var = varList.get(i);

				System.out.println(var + "=(" + viewName
						+ ")findViewById(R.id." + idName + ");");
			}

			System.out.println("}");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void fetchChildId(Element parent, ArrayList<String> viewList,
			ArrayList<String> idList) {
		String viewName = parent.getTagName();
		String idAttr = parent.getAttribute("android:id");
		if (idAttr != null && idAttr.length() > 0) {
			int index = idAttr.indexOf("/");
			if (index >= 0) {
				String id = idAttr.substring(index + 1);
				if (id != null && id.length() > 0) {
					viewList.add(viewName);
					idList.add(id);
				}
			}
		}

		NodeList childList = parent.getChildNodes();
		for (int i = 0; i < childList.getLength(); i++) {
			Node child = childList.item(i);
			if (child instanceof Element)
				fetchChildId((Element) child, viewList, idList);
		}
	}

}
